<template>
	<view>
		<!-- 申请进群 -->
		<view class="createGroup">
			<!-- 群简介 -->
			<view class="create_textarea">
				<view>
					<textarea @input="getGroupDec" placeholder="描述" placeholder-style="color:#CFCFCF;line-height:20px;font-size:12px;"/>
				</view>
			</view>
			
			<view class="create_btn">
				<button type="primary" @tap="applyGroup">申请</button>
			</view>
		</view>
	</view>
</template>
<script>
	import app from '@/App.vue';
	import {
		GYZJMODEL as gyzjModel
	} from '@/utils/gyzjModel.js';
	export default {
		data() {
			return {
				/*申请加入群的参数*/
				addFriendName: [],		
				roomId: "",				// 群id
				nameList:[],
			}
		},
		onLoad(options) {
			let addOptions=options.addOptions;
			let nameListOptions=options.nameListOptions;
			console.log('addOptions的值',addOptions);
			this.roomId=JSON.parse(addOptions)['roomId'];
			this.addFriendName=JSON.parse(addOptions)['list'];
			this.nameList=JSON.parse(nameListOptions);
		},
		onShow() {
			var me = this;
			// 获取当前用户的好友信息
			this.$im.conn.getRoster({
				success: function(roster){
					var member = [];
					for(let i = 0; i < roster.length; i++){
						if(roster[i].subscription == "both"){
							member.push(roster[i]);
						}
					}
					me.friendList = member
				}
			});
		},
		methods: {
			/*获取当前群的成员*/
			getGroupMember(id){
				console.log('进来获取群成员');
				var me = this;
				// 获取群成员
				var pageNum = 1,
					pageSize = 1000;
				var options = {
					pageNum: pageNum,
					pageSize: pageSize,
					groupId: id,
					success: function(resp){
						if(resp && resp.data && resp.data.data){
							console.log('进来获取群成员的值',resp.data.data);
							me.groupMember = resp.data.data
						}
						console.log(me.groupMember);
					},
					error: function(err){
			
					}
				};
				this.$im.conn.listGroupMember(options);
			},
			/*类型选择结束*/
			getGroupDec(e){
				this.groupDec = e.detail.value
			},
			// 邀请进群
			applyGroup(){
				
				/*重复加入逻辑,见groups的into_room方法*/
				let me=this,friendAttr = [],flagRes,options,groupMemberAll;
				var nameList = {
					myName: uni.getStorageSync("myUsername"),
					your: this.nameList.username,
					groupId: this.nameList.roomid
				};
				/*聊天之前判断设置成员*/
				// var me = this;
				// 获取群成员
				var pageNum = 1,
					pageSize = 1000;
				var option = {
					pageNum: pageNum,
					pageSize: pageSize,
					groupId: this.nameList.groupId,
					success: function(resp){
						if(resp && resp.data && resp.data.data){
							console.log('进来获取群成员的值',resp.data.data);
							groupMemberAll = resp.data.data;
							me.getGroupMember(me.nameList.groupId);
							console.log('之前的groupMember',groupMemberAll)
							if(groupMemberAll){
								friendAttr.push(nameList.myName);
								console.log('friendAttr的值',friendAttr);
								console.log('groupMemberAll的值',groupMemberAll);
							    flagRes=gyzjModel.prototype.isExistGroup(friendAttr, groupMemberAll);
								console.log('flagRes的值',flagRes);
								if(flagRes){
									uni.navigateTo({
										url: "../groupChatRoom/groupChatRoom?username="+JSON.stringify(nameList)
									});
								}else{
									options = {
										list: friendAttr,
										roomId: me.nameList.groupId,
									}
									console.log('options的值',JSON.stringify(options));
									
									uni.navigateTo({
										url: "../pay_groups/pay_groups?addOptions="+JSON.stringify(options)+"&nameListOptions="+JSON.stringify(me.nameList)
									});
								}
								
							}
							
						}
					},
					error: function(err){
							
					}
				};
				this.$im.conn.listGroupMember(option);
				
				
				// var firendArr = [],me=this,nameList;
				// firendArr.push(this.addFriendName);
				// nameList = {
				// 	myName: uni.getStorageSync("myUsername"),
				// 	your: this.addFriendName,
				// 	groupId: this.roomId
				// };
				// let option={
				// 	list: firendArr,
				// 	roomId: me.roomId,
				// 	success:function(){
				// 		uni.showToast({
				// 			title: "加入成功",
				// 			duration: 2000,
				// 		});
				// 		uni.navigateTo({
				// 			url: "../../groupChatRoom/groupChatRoom?username="+JSON.stringify(nameList)
				// 		});
				// 	},
				// 	error:function(){
				// 		uni.showToast({
				// 			title: err.data.error_description,
				// 		});
				// 	}
				// }
				// this.$im.conn.addGroupMembers(option);
			},
		}
	}
</script>

<style>

.createGroup{
	background-color: #CFCFCF;
}
.create_input {
	width: 100%;
	height: 40px;
	background-color: #ffffff;
	border-top: 1px solid #CFCFCF;
}
.create_input view {
	width: 100%;
	height: 100%;
	padding: 0 10px 0 10px;
	box-sizing: border-box;
}
.create_input input {
	font-size: 14px;
	width: 100%;
	height: 100%;
	line-height: 100%;
}

.create_textarea {
	width: 100%;
	height: 150px;
	background-color: #ffffff;
	border-top: 1px solid #CFCFCF;
}
.create_textarea view {
	width: 100%;
	height: 100%;
	line-height: 100;
	padding: 0 10px 0 10px;
	box-sizing: border-box;
}
.create_textarea textarea {
	padding-top: 7px;
	font-size: 14px;
	width: 100%;
	height: 100%;
	box-sizing: border-box;
	line-height: 20px;
}

/*  */
.check_area {
	margin-top: 1px;
}
.check_area .checkbox-group {
	background: #fff;
	width: 100%;
	margin: 0 auto;
	padding: 10px 10px 0 10px;
	box-sizing: border-box;
}
.check_area .checkbox-group:last-child {
	padding: 10px;
}
.check_area .checkbox-group .checkbox {
	display: block;
	font-size: 14px;
	line-height: 30px;
}
.invite_text {
	font-size: 14px;
	padding-left: 12px;
	color: #333;
}

/*  */
.friend_list {
	margin-top: 1px;
	background: #fff;
}
.friend_list .checkbox-group {
	background: #fff;
	padding: 10px;
	box-sizing: border-box;
}
.friend_list .checkbox-group .checkbox {
	display: block;
	font-size: 14px;
	line-height:30px;
	margin-bottom: 8px;
}

.create_btn {
	background-color: #fff;
}
.create_btn button {
	margin: 0 auto;
	width: calc(100% - 20px);
}

</style>
